<?php

namespace App\Jobs;

use App\Dictionary;
use App\Model\Classes;
use App\Model\ErrorTask;
use App\Model\User;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Symfony\Component\DomCrawler\Crawler;
use BindService;
use Log;
class SaveBindUser implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public $client;

    private $array;

    /**
     * Create a new job instance.
     * @param $array
     */
    public function __construct($array)
    {
        $this->array = $array;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->getInfo();
    }

    private function getInfo()
    {
        $client = new Client();
        try{
            $response =$client->get($this->getInfoUrl(), [
                'headers' => [
                    'Referer' => \App\Service\BindService::INDEX_URL,
                    'Cookie' => BindService::getCookie($this->array['sessionId'], $this->array['cookieId']),
                ]
            ]);
            $content = $response->getBody()->getContents();

            $this->analysis($content);

        }catch(\Exception $e){
            Log::error('获取用户信息错误' . $e->getMessage() . PHP_EOL . 'number:' . $this->array['number'] . PHP_EOL . 'password:' . $this->array['password']);
        }
    }

    private function getInfoUrl()
    {
        return \App\Service\BindService::INDEX_URL . 'xsgrxx.aspx?xh=' . $this->array['number'] . '&gnmkdm=N121501';
    }

    private function analysis($content)
    {
        $crawler = new Crawler($content);

        $name = $crawler->filter('#xm')->count() > 0 ? $crawler->filter('#xm')->text() : null;

        $gender = $crawler->filter('#lbl_xb')->count() > 0 ? ($crawler->filter('#lbl_xb')->text() === '男' ? 1 : 0) : null;

        $college = $crawler->filter('#lbl_xy')->count() > 0 ? $crawler->filter('#lbl_xy')->text() : null;

        $class = $crawler->filter('#lbl_xzb')->count() > 0 ? $crawler->filter('#lbl_xzb')->text() : null;

        $major = $crawler->filter('#lbl_zymc')->count() > 0 ? $crawler->filter('#lbl_zymc')->text() : null;

        $grade = $crawler->filter('#lbl_dqszj')->count() > 0 ? $crawler->filter('#lbl_dqszj')->text() : null;

        $area = $crawler->filter('#lbl_jg')->count() > 0 ? ($crawler->filter('#lbl_jg')->text() ? : $crawler->filter('#lbl_lydq')->text()) : null;

        $homeAddress = $crawler->filter('#lbl_jtszd')->count() > 0 ? $crawler->filter('#lbl_jtszd')->text() : ($crawler->filter('#jtszd')->count() > 0 ? $crawler->filter('#jtszd')->attr('value') : null);

        $highSchool = $crawler->filter('#lbl_byzx')->count() > 0 ? $crawler->filter('#lbl_byzx')->text() : null;

        $birthday = $crawler->filter('#lbl_csrq')->count() > 0 ? date('Y-m-d',strtotime($crawler->filter('#lbl_csrq')->text())) : null;

        $identityNumber = $crawler->filter('#lbl_sfzh')->count() > 0 ? $crawler->filter('#lbl_sfzh')->text() : null;

        $user = User::firstOrCreate(['open_id' => $this->array['openId']]);

        $class = Classes::firstOrCreate(['name' => $class, 'college' => $college]);

        $user->update([
            'open_id' => $this->array['openId'],
            'gender' => $gender,
            'name' => $name,
            'number' => $this->array['number'],
            'password' => $this->array['password'],
            'role' => $this->array['role'],
            'major' => $major,
            'class_id' => $class->id,
            'grade' => $grade,
            'area' => $area,
            'home_address' => $homeAddress,
            'high_school' => $highSchool,
            'birthday' => $birthday,
            'identity_number' => $identityNumber,
        ]);

    }
}
